package com.fivefu.core.hik.services.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.fivefu.base.common.utils.str.StrUtils;
import com.fivefu.core.hik.entity.DbHikDevice;
import com.fivefu.core.hik.entity.DbHikRegionsDevice;
import com.fivefu.core.hik.mapper.DbHikDeviceMapper;
import com.fivefu.core.hik.services.DbHikDeviceService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fivefu.core.hik.services.DbHikRegionsDeviceService;
import com.fivefu.module.dictionary.entity.DbSysDict;
import com.fivefu.module.dictionary.service.DbSysDictService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 海康设备表 服务实现类
 * </p>
 *
 * @author 
 * @since 2022-06-30 14:17:37
 */
@Service
public class DbHikDeviceServiceImpl extends ServiceImpl<DbHikDeviceMapper, DbHikDevice> implements DbHikDeviceService {

    @Autowired
    private DbSysDictService dbSysDictService;

    @Autowired
    private DbHikRegionsDeviceService dbHikRegionsDeviceService;

    /**
     * 通过资源唯一编码查询编码设备信息
     * @param indexCode 资源唯一编码
     * @return
     */
    public DbHikDevice queryDbHikDeviceByIndexCode(String indexCode) {
        QueryWrapper<DbHikDevice> wrapper = new QueryWrapper<>();
        wrapper.eq("index_code",indexCode);
        DbHikDevice device = baseMapper.selectOne(wrapper);
        return device;
    }
    /**
     * 编辑海康编码设备信息
     * @param device 编码设备信息
     */
    @Override
    public void editHikEncodeDevice(DbHikDevice device) {
        //资源唯一编码
        String indexCode = device.getIndexCode();
        //通过资源唯一编码查询编码设备信息
        DbHikDevice oldDevice = queryDbHikDeviceByIndexCode(indexCode);
        if (null != oldDevice) {
            device.setId(oldDevice.getId());
        }

        LocalDateTime time = LocalDateTime.now();
        device.setUpdatedTime(time);
        //判断编码设备是否删除
        if (null != device.getDeviceStatus()) {
            if (device.getDeviceStatus() < 0 ) {
                device.setIsDelete(1);
            } else {
                device.setIsDelete(0);
            }
        }

        //将编码设备中的接入协议由海康提供的转换为字典中保存的编号
        if (StrUtils.isNotNull(device.getTreatyType())) {
            //根据dict_value查询接入协议对应的dict_code
            DbSysDict dict = dbSysDictService.queryDictByValue("TTP", device.getTreatyType());
            if(dict != null){
                device.setTreatyType(dict.getDictCode());
            }
        }
        //将编码设备中的资源类型由海康提供的转换为字典中保存的编号
        if (StrUtils.isNotNull(device.getResourceType())) {
            String resourceType = device.getResourceType();
            String dictCode = queryDictCodes("RT",resourceType,",");
            device.setResourceType(dictCode);
        }
        //判断能力集是否为空
        if (StrUtils.isNotNull(device.getCapability())) {
            String capability = device.getCapability();
            //多能力集用”@“分割

            String dictCode = queryDictCodes("CP",capability,"@");
            device.setCapability(dictCode);
        }
        //判断厂商是否为空
        if (StrUtils.isNotNull(device.getManufacturer())) {
            if ("hikvision".equals(device.getManufacturer())) {
                device.setManufacturer("2");
            } else if ("dahua".equals(device.getManufacturer())) {
                device.setManufacturer("1");
            }
            DbSysDict dict = dbSysDictService.queryDictByValue("MP", device.getManufacturer());
            if (null != dict) {
                device.setManufacturer(dict.getDictCode());
            }
        }
        //编码设备id不为空则为更新
        if (null != device.getId()) {
            baseMapper.updateById(device);
        } else {
            device.setCreatedTime(time);
            if (null == device.getIsDelete()) {
                device.setIsDelete(0);
            }
            baseMapper.insert(device);
        }
    }

    /**
     * 获取数据字典编号
     * @param dictParentCode
     * @param dictValue
     * @param suffix
     * @return
     */
    public String queryDictCodes(String dictParentCode, String dictValue, String suffix){
        String[] dictValueArr = dictValue.split(suffix);
        List<DbSysDict> dictList = dbSysDictService.queryDictCodesByValue(dictParentCode, dictValueArr);
        List<String> list = dictList.stream().map(k -> k.getDictCode()).collect(Collectors.toList());
        return String.join(",", list);
    }


    /**
     * 查询所有海康设备数据
     * @return
     */
    @Override
    public List<DbHikDevice> queryAllDevice() {
        QueryWrapper<DbHikDevice> wrapper = new QueryWrapper<>();
        wrapper.eq("is_delete",0);
        List<DbHikDevice> resultList = baseMapper.selectList(wrapper);
        return resultList;
    }

    /**
     * 根据区域设备关系获取相应编码设备信息
     * @return
     */
    @Override
    public List<DbHikDevice> queryEffectiveDevice() {
        List<DbHikRegionsDevice> regionsDeviceList = dbHikRegionsDeviceService.queryRegionDevice();
        List<String> deviceCodes = regionsDeviceList.stream().map(k -> k.getIndexCode()).collect(Collectors.toList());
        QueryWrapper<DbHikDevice> wrapper = new QueryWrapper<>();
        wrapper.eq("is_delete",0);
        wrapper.in("index_code",deviceCodes);
        List<DbHikDevice> dbHikDevices = baseMapper.selectList(wrapper);
        return dbHikDevices;
    }

    /**
     * 删除设备数据信息
     * @param deviceIndexCodes
     * @param userId
     */
    @Override
    public void deleteHikDevice(List<String> deviceIndexCodes, String userId) {
        QueryWrapper<DbHikDevice> wrapper = new QueryWrapper<>();
        wrapper.eq("is_delete",0);
        if (!deviceIndexCodes.isEmpty()) {
            wrapper.notIn("index_code",deviceIndexCodes);
        }

        DbHikDevice device = new DbHikDevice();
        device.setUpdatedBy(userId);
        device.setUpdatedTime(LocalDateTime.now());
        device.setIsDelete(1);
        baseMapper.update(device,wrapper);
    }

}
